package com.daviteq.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daviteq.ems.entity.UserEntity;
import com.daviteq.user.service.UserService;
import com.google.gson.Gson;

@Controller
public class UserController {
	@Autowired
	private Gson gson;

	private static final Logger log = Logger.getLogger(UserController.class);
	
	
	@RequestMapping(value = { "/", "/index" })
	public String defaultPage(HttpServletRequest resRequest,
			HttpServletResponse response) {
		log.info("Web initiate");
		return "Index";
	}

	@RequestMapping(value = "/user", params = "action=getAllUser", method = RequestMethod.GET)
	@ResponseBody
	public String getAllUserMethod() {
		 /** get all account from  database**/
		UserService userService = new UserService();
		List<UserEntity> listAllUser = userService.getAll();
		return gson.toJson(listAllUser);
	}

	@RequestMapping(value = "/user", params = "action=addUser", method = RequestMethod.POST)
	@ResponseBody
	public String addUserMethod(
			@RequestParam("user") String userJson) {
		
		Gson gs = new Gson();
		UserService userService = new UserService();
		
		/** get date time current system **/
		Date todayD = new Date(System.currentTimeMillis());
		SimpleDateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String todayS = dayFormat.format(todayD.getTime());
		
		/** set fields UserEntity isn't set**/
		UserEntity user = gs.fromJson(userJson, UserEntity.class);
		user.setLastLogin(todayS);
		user.setCreatedDate(todayS);
		user.setLastUpdated(todayS);
		
		return gson.toJson(userService.insertUser(user));
	}

	@RequestMapping(value = "/user", params = "action=deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public String deleteUserMethod(
			@RequestParam("deleteID") String id) {
		boolean result=false;
		
		UserService userService = new UserService();
		if (id.equals("") == false) {
			result=userService.deleteUser(id);
		}
		
		return gson.toJson(result);
	}

	@RequestMapping(value = "/user", params = "action=editUser", method = RequestMethod.POST)
	@ResponseBody
	public String editUserMethod(
			@RequestParam("editID") String userEdit) {
		/** test userEdit id if not null and then update to database  **/
		if (userEdit.equals("") == false) {
			UserService userService = new UserService();
			UserEntity getUser = userService.getUserById(userEdit);
			return gson.toJson(getUser);
		}
		return null;
	}

	@RequestMapping(value = "/user", params = "action=updateUser", method = RequestMethod.POST)
	@ResponseBody
	public String updateUserMethod(@RequestParam("user") String userJson,
			@RequestParam("roleUser") String userRole) {
		
		/** test string role user if null print to screen console **/
		if (!userRole.equals("")) {
			String[] roles = userRole.split(",");
			for (String role : roles) {
				System.out.println(role);
			}
		}
		
		/** parser object user json data to Object user UserEntity **/
		UserEntity user = gson.fromJson(userJson, UserEntity.class);
		UserService userService = new UserService();
		
		return gson.toJson(userService.updateUser(user));
	}

	@RequestMapping(value = "/admin/index", method = RequestMethod.GET)
	@ResponseBody
	public String getAdminIndexPage() {
		return "Admin index page";
	}

	@RequestMapping(value = "/admin/view", method = RequestMethod.GET)
	@ResponseBody
	public String getAdminViewPage() {
		return "Admin view page";
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public String getTestMethod() {
		List<String> str = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			str.add("String " + i);
		}
		return gson.toJson(str);
	}
}
